package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.bean.BuyerCarBean;
import com.project.bean.GoodsBean;
import com.project.mapper.IBuyerCarMapper;
import com.project.mapper.IGoodsMapper;
import com.project.service.IBuyerCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class BuyerCarServiceImpl implements IBuyerCarService {
    @Autowired
    private IBuyerCarMapper mapper;
    @Autowired
    private IGoodsMapper goodsMapper;
    @Override
    public List<BuyerCarBean> findByUserId(Integer userId) {
        return mapper.findByUserId(userId);
    }

    @Override
    public void add(String option, GoodsBean goods, Integer num, Integer userId) {
                    QueryWrapper<BuyerCarBean> qw = new QueryWrapper<>();
                    qw.eq("fk_goodsId",goods.getNumber());
                    qw.eq("fk_userId",userId);
                    BuyerCarBean buyerCarBean = mapper.selectOne(qw);
                    goodsMapper.updatePrice(goods.getNumber(),goods.getPrice());
                    if (buyerCarBean==null){
                        buyerCarBean=new BuyerCarBean(goods.getNumber(),userId,goods.getPrice(),num);
                        mapper.insert(buyerCarBean);
                    }else {
                        buyerCarBean.setNum(num+buyerCarBean.getNum());
                        mapper.updateById(buyerCarBean);
                    }
        if (option.equals("add")){
            GoodsBean goodsBean = goods;
            goodsBean .setNum(0);
            goodsBean .setSellPrice(0);
            goodsMapper.insert(goodsBean);
        }

    }

    @Override
    public void del(Integer buyCarId) {
          mapper.deleteById(buyCarId);
    }

    @Override
    public void update(Integer byCarId, Integer num) {
         BuyerCarBean buyerCarBean = new BuyerCarBean();
         buyerCarBean.setId(byCarId);
         buyerCarBean.setNum(num);
        mapper.updateById(buyerCarBean);
    }
}
